package com.san.metadata.task;

import java.lang.reflect.Method;
import java.util.Date;
import java.util.List;

import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.springframework.context.ApplicationContext;
import org.springframework.util.ReflectionUtils;

import com.san.common.config.ApplicationContextRegister;
import com.san.common.utils.Constant;
import com.san.metadata.domain.OperationJobDO;

/**
 * 操作元数据单次执行调度线程
 * @author xiangyz
 *
 */
public class OperationMetaDataThread extends Thread{
	
	private static final Logger logger = LoggerFactory.getLogger(OperationMetaDataThread.class);
	
	
	private List<OperationJobDO> list;
	
	public OperationMetaDataThread(List<OperationJobDO> list) {
		this.list = list;
	}
	
	@Override
	public void run() {
		Date date = new Date();
		Long batchNo = date.getTime();
		
		for (OperationJobDO jobDO : list) {
			if (jobDO.getStatus() == Constant.STATUS_NORMAL) {
				String beanName = null;
				String methodName = null;
				if (Constant.JOB_TYPE_INTERFACE == jobDO.getJobType()) {
					beanName = jobDO.getSpringBean();
					methodName = jobDO.getMethodName();
				} else if (Constant.JOB_TYPE_DATA_SYNC == jobDO.getJobType()) {
					beanName = "operationMetadataService";
					methodName = "dataSync";
				} else if (Constant.JOB_TYPE_QICHACHA == jobDO.getJobType()) {
					beanName = "operationMetadataService";
					methodName = "qichachaCollect";
				} else {
					logger.error("未知作业类型，作业ID："+jobDO.getJobId());
					break;
				}
				if (null == beanName || null == methodName) {
					logger.error("spring bean或者方法名为空："+jobDO.getJobId());
					break;
				}
				ApplicationContext applicationContext = ApplicationContextRegister.getApplicationContext();
				Object obj = applicationContext.getBean(beanName);
				Method method = ReflectionUtils.findMethod(obj.getClass(), methodName, Long.class,
						Long.class);
				ReflectionUtils.invokeMethod(method, obj, batchNo, jobDO.getJobId());
			}
		}
	}

}
